"""Adding role column to user table

Revision ID: 538eeb160af6
Revises: 6b9d673d8e30
Create Date: 2015-09-17 04:22:21.262285

"""

# revision identifiers, used by Alembic.
revision = '538eeb160af6'
down_revision = '6b9d673d8e30'

from alembic import op
from flask_sqlalchemy import _SessionSignalEvents
import sqlalchemy as sa
from sqlalchemy import event
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, Session as BaseSession, relationship

Session = sessionmaker()

#event.remove(BaseSession, 'before_commit', _SessionSignalEvents.session_signal_before_commit)
#event.remove(BaseSession, 'after_commit', _SessionSignalEvents.session_signal_after_commit)
#event.remove(BaseSession, 'after_rollback', _SessionSignalEvents.session_signal_after_rollback)

Base = declarative_base()

class User(Base):
    __tablename__ = 'user'
    id = sa.Column(sa.Integer, primary_key=True)
    active = sa.Column(sa.Boolean())
    role = sa.Column(sa.String(30), default="View")


def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    ### end Alembic commands ###

    bind = op.get_bind()
    session = Session(bind=bind)
    op.add_column('user', sa.Column('role', sa.String(length=30), nullable=True))
    for user in session.query(User):
        if user.active:
            user.role = 'View'

    session.commit()


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('user', 'role')
    ### end Alembic commands ###
